var loading = false; //状态标记+
var app = new Vue({
    el: '#app',
    data: {
        onLoading:true,//页面加载中
        //详情
        assDetails: {
            list: [],
        },
        //评论列表
        commentList: { list: [], total: 0 },
        //详情ID
        objectId: "",
        //评论输入框
        messageval: "",
        hideing: true,//默认正在加载不显示
        //底部广告图
        admList: '',
        isShare: 0, //是否为分享进来的页面  默认0否  1是
        //用户ID
        userId: 0,
        pageIndex: 1,
        pageSize: 5,//每次加载5页
        appShareTitle:'',//app分享后显示的标题
        isDown:true,//是否显示下载提示弹出
    },
    created() {
        var that = this;
        FastClick.attach(document.body);
        //获取用户ID
        if ($.getPara("userId") != "") {
            var userId = $.getPara("userId");
            that.userId = userId;
            // console.log(that.userId);
            //缓存用户ID
            $.setStorage("userId", userId);
        } else {
            if ($.getStorage("userId") && $.getStorage("userId") != "") {
                var userId = parseInt($.getStorage("userId"));
                that.userId = userId;
            }
        }
        if($.getPara("isShare") != "" ){
        	this.isShare = $.getPara("isShare");
        }
        //url获取详情ID
        that.objectId = $.getPara('id');
        //获取底部广告图
        that.getFixedAdm();
        //获取详情
        that.getDetails(that.objectId);
        //获取评论列表
        that.getCommentList();

        $("#asso-news li").last().css("border-bottom", "none");

    },
    mounted() {
        var that = this;
        this.$nextTick(function () {
            document.querySelector('#inputId').scrollIntoView();
            //只要在input的点击事件，或者获取焦点的事件中，加入这个api就好了		
            $(document.body).infinite().on("infinite", function () {
                if (loading) return;
                loading = true;
                that.hideing = false;
                setTimeout(function () {
                    // console.log("已经加载出来了+")
                    var pageIndex = that.pageIndex;
                    pageIndex++;
                    that.pageIndex = pageIndex;
                    that.getCommentList();
                    loading = false;
                }, 1500);   //模拟延迟
            });

        })

    },
    methods: {
        backTop() {
            window.history.go(-1);
        },
        //获取详情
        getDetails(id) {
            var that = this;
            var id = id;
            // var userId = getUserId(false);
            $.ajax({
                type: "GET",
                url: "/api/exhibition/detail",
                dataType: 'json',
                data: {
                    id: id,
                    userId: that.userId
                },
                success: function (res) {
                    // console.log(res);
                    if (res.code == 200) {
                        res.data.keywords = JSON.parse(res.data.keywords);
                        that.assDetails = res.data;
                        that.appShareTitle = res.data.title;
                        setTimeout(function(){
                            // that.$options.methods.changeTitle();//在test3中调用test2的方法 
                            that.changeTitle();//在test3中调用test2的方法 
                            that.onLoading = false;
                        },1000)
                    } else {
                        $.toast("异常", "txt");
                    }
                },
            })
        },
        //获取广告图
        getFixedAdm() {
            var that = this;
            $.ajax({
                type: "GET",
                url: "/api/banner-ad/ad/byPositionType",
                dataType: 'json',
                data: {
                    positionType: 16,
                },
            }).done(function (data, status, xhr) {
                if (data.code == 200) {
                    that.admList = data;
                } else {
                    $.toast("异常", "txt");
                }
            });
        },
        //评论列表
        getCommentList() {
            var that = this;
            var objectId = that.objectId;
            var userId = that.userId;
            $.ajax({
                type: "GET",
                url: "/api/comment/common/list",
                dataType: 'json',
                data: {
                    type: 11,
                    objectId: objectId,
                    userId: userId,
                    pageIndex: that.pageIndex,
                    pageSize: that.pageSize
                },
                success: function (res) {
                    if (res.code == 200) {
                        var list = res.data.list;
                        that.commentList.total = res.data.total;
                        // that.commentList.list = res.data.list;
                        if (that.commentList.list.length > 0) {  //有评论数据，加载更多
                            if (list.length > 0) {
                                var original = that.commentList.list;
                                var array = original.concat(list);
                                that.commentList.list = array;
                            } else {
                                loading = true;
                                that.hideing = true;
                                $.toast("没有更多了...", "text");
                            }
                            //更新状态
                            that.hideing = true;
                        } else {  //暂无评论数据
                            that.commentList.list = res.data.list;
                            that.hideing = true;
                        }
                    } else {
                        //添加数据
                        $.toast(res.msg, "text");
                    }
                    //that.commentList = res.data;
                },
            })
        },
        //收藏
        chooseCollect() {
            //添加一个强制登录以获取userId
            //获取到userId字段内容并且用户id>0已登录   
            if (this.userId && this.userId > 0) {
                var that = this;
                if (that.userId != false) {
                    //1资讯，2供应，3求购，4设备，5印厂，6协会，7媒体，8学院，9评测，10动态，11活动，12故事回忆，13心情
                    $.ajax({
                        type: "POST",
                        url: "/api/collect/collect",
                        dataType: 'json',
                        data: {
                            type: 14,
                            objectId: that.objectId,
                            userId: that.userId,
                        },
                        success: function (res) {
                            if (res.code == 200) {
                                $.toast("收藏成功", "text");
                                that.$set(that.assDetails, "is_collect", 1)
                            } else {
                                $.toast("再试一次吧", "text");
                            }
                        },
                    })
                } else {
                    $.toast("请先登录", "text");
                }
            } else {
                //用户未登录
                if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
                    //默认为iOS系统   
                    window.webkit.messageHandlers.appLogin.postMessage(null);
                } else {
                    //pc
                    alert("PC浏览器-appLogin");
                };
            }
        },
        //取消收藏
        chanceCollect() {
            //添加一个强制登录以获取userId
            //获取到userId字段内容并且用户id>0已登录   
            if  (this.userId && this.userId > 0) {
                var that = this;
                if (that.userId != false) {
                    //1资讯，2供应，3求购，4设备，5印厂，6协会，7媒体，8学院，9评测，10动态，11活动，12故事回忆，13心情
                    $.ajax({
                        type: "POST",
                        url: "/api/collect/cancelCollect",
                        dataType: 'json',
                        data: {
                            type: 14,
                            objectId: that.objectId,
                            userId: that.userId,
                        },
                        success: function (res) {
                            if (res.code == 200) {
                                $.toast("取消收藏成功", "text");
                                that.$set(that.assDetails, "is_collect", 0)
                            } else {
                                $.toast("再试一次吧", "text");
                            }
                        },
                    })
                } else {
                    $.toast("请先登录", "text");
                }
            } else {
                //用户未登录
                if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
                    //默认为iOS系统   
                    window.webkit.messageHandlers.appLogin.postMessage(null);
                } else {
                    //pc
                    alert("PC浏览器-appLogin");
                };
            }
        },
        //发表评论
        //评论对象类型：1故事回忆，2话题，3学院，4协会，5媒体，6心情，7动态，8评测，9活动，10印厂，11展会
        sendMessage() {
            //添加一个强制登录以获取userId
            //获取到userId字段内容并且用户id>0已登录   
            if ( this.userId && this.userId > 0) {
                var that = this;
                var val = that.messageval;
                var objectId = that.objectId;
                var userId = that.userId;
                if (userId != false && userId != 0) {
                    if (val == "") {
                        $.toast("请填写评论", "text");
                        return false;
                    };
                    $.ajax({
                        type: "POST",
                        url: "/api/comment/common/publish",
                        dataType: 'json',
                        data: {
                            type: 11,
                            objectId: objectId,
                            userId: userId,
                            comment: val
                        },
                        success: function (res) {
                            if (res.code == 200) {
                                $.toast("评论成功，等待审核", "text");
                                that.commentList.list = [];//清空原有数据
                                that.pageIndex = 1;//重置第一页						
                                that.getCommentList();
                                that.messageval = "";
                                loading = false;
                            }
                        },
                    })
                } else {
                    $.toast("请先登录", "text");
                }
            } else {
                //用户未登录
                if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
                    //默认为iOS系统   
                    window.webkit.messageHandlers.appLogin.postMessage(null);
                } else {
                    //pc
                    alert("PC浏览器-appLogin");
                };
            }
        },
        //路径跳转--展会详情
        urlHref(item) {
            var para = {};
            para.title = "展会详情";
            para.type = "exhibitionDetails";
            para.url = '/PageClassify/html/Exhibition/exhibitionDetails.html?id=' + item.id;
            //window.location.href =para.url;
            if (this.userId > 0) {
                para.url = '/PageClassify/html/Exhibition/exhibitionDetails.html?id=' + item.id + "&userId=" + this.userId;
            }
            if (/(Android)/i.test(navigator.userAgent)) { //判断Android
                //是android系统
                // alert("Android系统");
                jsAndroid.appPush(JSON.stringify(para))
            } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
                //默认为iOS系统
                window.webkit.messageHandlers.appPush.postMessage(para);
            } else {
                //pc
                window.location.href = para.url;
                alert("PC浏览器");
            };
        },
        //调用原生分享
        appShare(item) {
            /*参数：
            标题：title
            内容：content
            图片：imgurl
            路径：url*/
            var para = {};
            para.title = item.title;
            para.content = "来自印刷搜搜的分享";
            para.imgurl = item.head_img;
            $("title").html(item.title);
            /*
                分享出去的页面仅做展示效果，无法处理用户登录操作
                因此，userId需要传0
                isShare为是否分享标识，1:是分享页面，
                H5端要在有分享功能的页面用url的isShare==1判断是否隐藏需要登录才能显示或者操作的区域
            */
            // console.log(para);
            para.url = '/PageClassify/html/Exhibition/exhibitionDetails.html?isShare=1&id=' + item.id + "&userId=0";
            //window.location.href = para.url;
            if (/(Android)/i.test(navigator.userAgent)) { //判断Android
                //是android系统  目前协议 安卓只能传字符串过去
                // alert("Android系统");
                jsAndroid.appShare(JSON.stringify(para))
                // jsAndroid.appPush("123456")
            } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
                //默认为iOS系统   
                window.webkit.messageHandlers.appShare.postMessage(para);
            } else {
                //pc
                window.location.href = para.url;
                alert("PC浏览器");
            };
        },
          //判断是否是微信浏览器的函数
          isWeiXin(){
            //window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息，这个属性可以用来判断浏览器类型
            var ua = window.navigator.userAgent.toLowerCase();
            //通过正则表达式匹配ua中是否含有MicroMessenger字符串
            if(ua.match(/MicroMessenger/i) == 'micromessenger'){
                return true;
            }else{
                return false;
            }
        },
        //动态修改title
        changeTitle(){
            let that = this;
            //动态修改title
            let t = that.appShareTitle;
            // console.log(t);
            //判断是否微信环境
            if(that.isWeiXin()){
                // 是微信浏览器，执行操作
                // alert("微信");
                var $body = $('body');
                // document.title = 'the title you want to set';
                document.title = t;
                var $iframe = $("<iframe style='display:none;'></iframe>");
                $iframe.on('load',function() {
                setTimeout(function() {
                $iframe.off('load').remove();
                }, 0);
                }).appendTo($body);
            }else{
            // 　　alert("不是微信");
                document.title  = t;
            }
        },
        //下载app
        downApp(){
            console.log("我要下载了");
            if (/(Android)/i.test(navigator.userAgent)) { //判断Android
                //是android系统  目前协议 安卓只能传字符串过去
                // alert("Android系统");
                window.location.href = "http://sj.qq.com/myapp/detail.htm?apkName=com.meijiang.printsearch";
            } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
                //默认为iOS系统   
                window.location.href = "https://itunes.apple.com/cn/app/%E5%8D%B0%E5%88%B7%E6%90%9C%E6%90%9C/id1415458460?mt=8";
            } else {
                alert("PC浏览器");
            };
        },
    }
})